//
//  ZPublishDetailsViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/14.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZPublishDetailsViewController.h"
#import "JNavigationController.h"
#import "JTAlertView.h"
#import "LZLoginViewController.h"
#import "ZAgainPostView.h" // 再次提交的提示框
#import "ZDepartmentsViewController.h" // 选择科室的详情界面
@interface ZPublishDetailsViewController ()<ZDeparDelegate>/*选择科室*/{
    
    NSArray * _nameArray;
    NSArray * _sexArray;
    UILabel * _promptLabel; //顶上的提示
    UILabel * _keShiLabel;
    UILabel * _ageLabel;
    UILabel * _sexlabel;
    UILabel * _manLabel;
    UILabel * _womanLabel;
    UILabel * _photoLabel;

    UIButton * _choseKSBtn;//选择科室
    UITextField * _ageTextFiled;// 填年龄
    
    UIButton * _manBtn;
    UIButton * _womenBtn;
    UIImageView  * _manImage;
    UIImageView *  _womenImage;
    
    UIButton * _photoBtn; // 模糊按钮
    UIImageView * _photoImaegView; // 因为要求全行可以点🐔
    
    UIButton * _corroborationBtn;// 确认发布
}
@property (nonatomic,strong) ZAgainPostView * againPostView;
@property (nonatomic,copy) NSString * kid;
@property (nonatomic,copy) NSString * sex;
@property (nonatomic,copy) NSString * keShi;//科室
@property (nonatomic,copy) NSString * is_dim;// 是否模糊
@property (nonatomic,strong)LZUserInfo * userInfo;
@end

@implementation ZPublishDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialzeDatas];
    [self initlizeInterface];
}


- (void)initialzeDatas {
    _nameArray = @[@"选择科室",@"病人年龄",@"病人性别",@"模糊照片"];
    _sexArray = @[@"男",@"女"];
    _userInfo = [LZUserInfo getUserInfo];
    _kid = @"";
    _sex = @"";
    _keShi = @"";
    _is_dim = @"0";
}
- (void)initlizeInterface {
    self.navigationItem.title = @"发布";
    self.view.backgroundColor = [UIColor whiteColor];
    [self makeSubViews];
    [self addSubViewsAndMasnoy];
}
- (void)makeSubViews {
//    __weak typeof(self) weakSelf = self;
    _promptLabel  = [[UILabel alloc] init];
    [self MakeThelabel:_promptLabel backColor:KHColorFromRGB(0xeaf6e7) color:KHColorFromRGB(0x4bb92c) Fontsize:15 context:@"温馨提示:选填病人资料，便于医生更准确回复" isCenter:NO];
    _keShiLabel = [[UILabel alloc] init];
    [self MakeThelabel:_keShiLabel backColor:[UIColor clearColor] color:KHColorFromRGB(0x333333) Fontsize:15 context:_nameArray[0] isCenter:NO];
    _ageLabel = [[UILabel alloc] init];
    [self MakeThelabel:_ageLabel backColor:[UIColor clearColor] color:KHColorFromRGB(0x333333) Fontsize:15 context:_nameArray[1] isCenter:NO];
    
    _sexlabel = [[UILabel alloc] init];
    [self MakeThelabel:_sexlabel backColor:[UIColor clearColor] color:KHColorFromRGB(0x333333) Fontsize:15 context:_nameArray[2] isCenter:NO];
    _photoLabel = [[UILabel alloc] init];
    [self MakeThelabel:_photoLabel backColor:[UIColor clearColor] color:KHColorFromRGB(0x333333) Fontsize:15 context:_nameArray[3] isCenter:NO];
    _manLabel = [[UILabel alloc] init];
    [self MakeThelabel:_manLabel backColor:[UIColor clearColor] color:KHColorFromRGB(0x333333) Fontsize:15 context:_sexArray[0] isCenter:YES];
    _womanLabel = [[UILabel alloc] init];
    [self MakeThelabel:_womanLabel backColor:[UIColor clearColor] color:KHColorFromRGB(0x333333) Fontsize:15 context:_sexArray[1] isCenter:YES];
    _choseKSBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_choseKSBtn addTarget:self action:@selector(choseKSBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_choseKSBtn setBackgroundColor:[UIColor clearColor]];
    [_choseKSBtn setTitleColor:KHGaryColor forState:UIControlStateNormal];
    [_choseKSBtn setTitle:@"点击选择" forState:UIControlStateNormal];
    _choseKSBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    _ageTextFiled = [[UITextField alloc] init];
    _ageTextFiled.placeholder = @"点击输入";
    _ageTextFiled.textAlignment = NSTextAlignmentCenter;
    
    _ageTextFiled.font = [UIFont systemFontOfSize:15];
    _ageTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    
    _manBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_manBtn addTarget:self action:@selector(ManButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _manImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newboy1"]];
    
    _womenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_womenBtn addTarget:self action:@selector(WomenButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _womenImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newgirl1"]];
    _photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _photoBtn.selected = NO;
    [_photoBtn addTarget:self action:@selector(vaguePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _photoImaegView = [[UIImageView alloc] init];
    _photoImaegView.image = [UIImage imageNamed:@"jubao_gou"];
    
    _corroborationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self MakeTheButton:_corroborationBtn color:KHColorFromRGB(0x4bb92c) context:@"确认发布" image:[UIImage new]];
    [_corroborationBtn addTarget:self action:@selector(corroBorationBtnAction:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)addSubViewsAndMasnoy {
    [self.view addSubview:_photoBtn];
    [self.view addSubview:_promptLabel];
    [self.view addSubview:_keShiLabel];
    [self.view addSubview:_ageLabel];
    [self.view addSubview:_sexlabel];
    [self.view addSubview:_photoLabel];
    [self.view addSubview:_choseKSBtn];
    [self.view addSubview:_ageTextFiled];
    [self.view addSubview:_manBtn];
    [self.view addSubview:_manLabel];
    [self.view addSubview:_manImage];
    [self.view addSubview:_womenBtn];
    [self.view addSubview:_womanLabel];
    [self.view addSubview:_womenImage];
    [self.view addSubview:_photoImaegView];
    [self.view addSubview:_corroborationBtn];
    
    [_promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.centerX.equalTo(self.view);
        make.height.offset(50*ysIZE);
        make.top.equalTo(self.view).offset(4*ysIZE);
    }];
    [_keShiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(10*xsIZE);
        make.top.equalTo(_promptLabel.mas_bottom).offset(20*ysIZE);
        make.height.mas_offset(15*ysIZE);
        make.width.equalTo(self.view).multipliedBy(0.4);
    }];
    [_ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(_keShiLabel);
        make.top.equalTo(_keShiLabel.mas_bottom).offset(40*ysIZE);
        
    }];
    [_sexlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(_ageLabel);
        make.top.equalTo(_ageLabel.mas_bottom).offset(40*ysIZE);
    }];
    [_photoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(_sexlabel);
        make.top.equalTo(_sexlabel.mas_bottom).offset(40*ysIZE);
    }];
    [_corroborationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.width.equalTo(self.view);
        make.height.offset(50*ysIZE);
    }];
    [_choseKSBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-10*xsIZE);
        make.centerY.height.equalTo(_keShiLabel);
        make.width.equalTo(self.view).multipliedBy(0.25);
    }];
    [_ageTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.equalTo(_ageLabel);
        make.width.right.equalTo(_choseKSBtn);
        
    }];
    [_womanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_ageTextFiled);
        make.centerY.height.equalTo(_sexlabel);
        make.width.offset(15*xsIZE);
    }];
    [_womenImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_womanLabel.mas_left).offset(-3*xsIZE);
        make.centerY.equalTo(_sexlabel);
        make.width.equalTo(_womanLabel);
        make.height.equalTo(_womenImage.mas_width);
    }];
    [_womenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_womanLabel);
//        make.centerY.equalTo(_sexlabel);
        make.left.equalTo(_womenImage.mas_left);
        make.top.equalTo(_ageLabel.mas_bottom).offset(20*ysIZE);
        make.bottom.equalTo(_sexlabel).offset(20*ysIZE);
    }];
    [_manLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_womenImage.mas_left).offset(-3*xsIZE);
        make.centerY.width.height.equalTo(_womanLabel);
    }];
    [_manImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_manLabel.mas_left).offset(-3*xsIZE);
        make.centerY.equalTo(_sexlabel);
        make.width.equalTo(_manLabel);
        make.height.equalTo(_manImage.mas_width);
    }];
    [_manBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_manLabel);
//        make.centerY.height.equalTo(_sexlabel);
        make.left.equalTo(_manImage);
        make.top.equalTo(_ageLabel.mas_bottom).offset(20*ysIZE);
        make.bottom.equalTo(_sexlabel).offset(20*ysIZE);
    }];
    [_photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(self.view);
        make.height.offset(60*ysIZE);
        make.centerY.equalTo(_photoLabel);
    }];
    [_photoImaegView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.width.height.equalTo(_womanLabel);
        make.centerY.equalTo(_photoLabel);
    }];
    [self addGrayView];// 添加灰色的线和其约束
}

- (void)addGrayView {
    UIView * grayView1 = [[UIView alloc] init];
    grayView1.backgroundColor = MYGRAYCOLOR;
    UIView * grayView2 = [[UIView alloc] init];
    grayView2.backgroundColor = MYGRAYCOLOR;
    UIView * grayView3 = [[UIView alloc] init];
    grayView3.backgroundColor = MYGRAYCOLOR;
    UIView * grayView4 = [[UIView alloc] init];
    grayView4.backgroundColor = MYGRAYCOLOR;
    UIView * grayView5 = [[UIView alloc] init];
    grayView5.backgroundColor = MYGRAYCOLOR;
    [self.view addSubview:grayView1];
    [self.view addSubview:grayView2];
    [self.view addSubview:grayView3];
    [self.view addSubview:grayView4];
    [self.view addSubview:grayView5];
    [grayView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.left.equalTo(self.view);
        make.bottom.equalTo(_promptLabel.mas_top);
    }];
    [grayView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.9);
        make.bottom.equalTo(_keShiLabel).offset(20*ysIZE);
        make.height.offset(1.5*xsIZE);
    }];
    [grayView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.9);
        make.bottom.equalTo(_ageLabel).offset(20*ysIZE);
        make.height.offset(1.5*xsIZE);
    }];
    [grayView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.9);
        make.bottom.equalTo(_sexlabel).offset(20*ysIZE);
        make.height.offset(1.5*xsIZE);
    }];
    [grayView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.width.equalTo(self.view);
        make.bottom.equalTo(_corroborationBtn.mas_top);
        make.top.equalTo(_photoLabel.mas_bottom).offset(20*ysIZE);
    }];
    
}
// 男女按钮的方法
- (void)ManButtonAction:(UIButton *)sender {

    _manImage.image = [UIImage imageNamed:@"newboy2"];
    _womenImage.image = [UIImage imageNamed:@"newgirl1"];
    _sex = @"男";
}

- (void)WomenButtonAction:(UIButton *)sender {
    _manImage.image = [UIImage imageNamed:@"newboy1"];
    _womenImage.image = [UIImage imageNamed:@"newgirl2"];
    _sex = @"女";
}
// 模糊按钮
- (void)vaguePhotoAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSString * imageName = sender.selected? @"jubao_gou_pre" : @"jubao_gou";
    _is_dim = sender.selected? @"1" : @"0";
    _photoImaegView.image = [UIImage imageNamed:imageName];
    
}
//确认发布按钮的方法
- (void)corroBorationBtnAction:(UIButton *)sender {
    if (!_userInfo.uid || [_userInfo.uid isEqualToString:@""]) {
        [self zpdPointOutLogin];
        return;
    }
    
    [self informationIssueNetWorking];
    
    
}
// 弹出登录窗
- (void)zpdPointOutLogin {
    JTAlertView * alert = [[JTAlertView alloc] init];
    [alert showPleaseLoginFromviewController:self loginHandel:^{
        LZLoginViewController * logVC = [[LZLoginViewController alloc] init];
        JNavigationController * navVc = [[JNavigationController alloc] initWithRootViewController:logVC];
        [self presentViewController:navVc animated:YES completion:nil];
    }];
}
//进入选择科室界面
- (void)choseKSBtnAction:(UIButton *)sender {
    ZDepartmentsViewController * ZDVC = [[ZDepartmentsViewController alloc] init];
    ZDVC.ZDeparDelegate = self;
    [self.navigationController pushViewController:ZDVC animated:YES];
}
// 选择科室后的回调
- (void)ZDepartmentWithString:(NSString *)string withID:(NSString *)kid {
    [_choseKSBtn setTitle:string forState:UIControlStateNormal];
    _kid = kid;
    _keShi = string;
    return;
}

- (void)informationIssueNetWorking{
    if ([self thenBeginFile]) {
        NSMutableDictionary * dict = [NSMutableDictionary dictionary];
        for (NSString * key in _infoDict.allKeys) {
            [dict setObject:_infoDict[key] forKey:key];
        }
        [dict setObject:_userInfo.uid forKey:@"uid"];
        [dict setObject:_keShi forKey:@"office"];
        [dict setObject:_ageTextFiled.text forKey:@"age"];
        [dict setObject:_sex forKey:@"sex"];
        [dict setObject:_is_dim forKey:@"is_dim"];
        _corroborationBtn.userInteractionEnabled = NO;
        [YJProgressHUD showProgress:@"发布问题中" inView:self.view];
        [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Index/addProblem" sucessBlock:^(id content) {
            NSDictionary * iDict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",iDict);
            _corroborationBtn.userInteractionEnabled = YES;
            if ([iDict[@"code"] intValue] == 1) {
                [YJProgressHUD showMessage:@"发布成功" inView:self.view];
                [self backHomeWithProblems:iDict];
            }else{
                [YJProgressHUD showMessage:@"问题发布失败" inView:self.view];

            }
        } failure:^(NSError *error) {
            [YJProgressHUD showMessage:@"网络请求错误" inView:self.view];
        }];
    }
    
   
}
// 判断输入格式
- (BOOL)thenBeginFile {
    
    if (!_keShi||[_keShi isEqualToString:@""]) {
        [YJProgressHUD showMessage:@"未选择科室" inView:self.view];
        return NO;
    }else if (!_ageTextFiled.text || [_ageTextFiled.text integerValue]<= 0 ||[_ageTextFiled.text integerValue] >100 || [_ageTextFiled.text isEqualToString:@""]) {
         [YJProgressHUD showMessage:@"年龄未输入或格式不符合" inView:self.view];
        return NO;
    }else if (!_sex || [_sex isEqualToString:@""]) {
        [YJProgressHUD showMessage:@"未选择性别" inView:self.view];
        return NO;
    }
    return YES;
}

//// 返回主页
- (void)backHomeWithProblems:(NSDictionary *)dict{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"issueExampleNotification" object:nil userInfo:dict];
    
}


//  设置label 和button的方法
- (void)MakeThelabel:(UILabel *)sender backColor:(UIColor *)backColor color:(UIColor *)Pcolor Fontsize:(NSInteger)size context:(NSString *)text isCenter:(BOOL)center{
    sender.text = text;
    sender.textColor = Pcolor;
    sender.backgroundColor = backColor;
    sender.font = [UIFont systemFontOfSize:size];
    sender.textAlignment = center?  NSTextAlignmentCenter: NSTextAlignmentLeft;
    
}
- (void)MakeTheButton:(UIButton *)sender color:(UIColor *)Pcolor context:(NSString *)title image:(UIImage *)image {
    [sender setImage:image forState:UIControlStateNormal];
    [sender setBackgroundColor:Pcolor];
    [sender setTitle:title forState:UIControlStateNormal];
    
}



// lazy

//- (ZAgainPostView *)againPostView {
//    if (_againPostView) {
//        return _againPostView;
//    }
//    _againPostView = [[ZAgainPostView alloc] init];
//    _againPostView.frame = CGRectMake(0, 0, SCREEN_Width, SCREEN_Height);
//
//       return _againPostView;
//}


@end
